#include "BufferUtil.h"

//从缓冲区中解析连续2个字节的数据为quint16值(ui16Value=首字节*256+次字节,指针pData位置没有发送变化)
quint16 CBufferUtil::GetQuint16(char* pData)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData);
    char secondByte = *(char*)(pData + 1);

    quint16 uiValue = (firstByte << 8) + secondByte;

    return uiValue;
}

quint16 CBufferUtil::DecodeQuint16(char* pData, int& iDataIndex)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char secondByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;

    quint16 uiValue = (firstByte << 8) + secondByte;

    return uiValue;
}

//从缓冲区中解析连续2个字节的数据为qint16值(i16Value=首字节*256+次字节,指针pData位置没有发送变化)
qint16 CBufferUtil::GetQint16(char* pData)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData);
    char secondByte = *(char*)(pData + 1);

    qint16 iValue = (firstByte << 8) + secondByte;

    return iValue;
}

qint16 CBufferUtil::DecodeQint16(char* pData, int& iDataIndex)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char secondByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;

    qint16 iValue = (firstByte << 8) + secondByte;

    return iValue;
}

//从缓冲区中解析连续4个字节的数据为quint32值(ui32Value=首字节*256*256*256+次字节*256*256+三字节*256+四字节,指针pData位置没有发送变化)
quint32 CBufferUtil::GetQuint32(char* pData)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData);
    char secondByte = *(char*)(pData + 1);
    char thirdByte = *(char*)(pData+2);
    char fouthByte = *(char*)(pData+3);

    quint32 uiValue = (firstByte << 24) + (secondByte << 16) + (thirdByte << 8) + fouthByte;

    return uiValue;
}

quint32 CBufferUtil::DecodeQuint32(char* pData, int& iDataIndex)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char secondByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char thirdByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char fouthByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;

    quint32 uiValue = (firstByte << 24) + (secondByte << 16) + (thirdByte << 8) + fouthByte;

    return uiValue;
}

//从缓冲区中解析连续4个字节的数据为qint32值(i32Value=首字节*256*256*256+次字节*256*256+三字节*256+四字节,指针pData位置没有发送变化)
qint32 CBufferUtil::GetQint32(char* pData)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData);
    char secondByte = *(char*)(pData + 1);
    char thirdByte = *(char*)(pData+2);
    char fouthByte = *(char*)(pData+3);

    qint32 iValue = (firstByte << 24) + (secondByte << 16) + (thirdByte << 8) + fouthByte;

    return iValue;
}

qint32 CBufferUtil::DecodeQint32(char* pData, int& iDataIndex)
{
    if (0 == pData) return 0;

    char firstByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char secondByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char thirdByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    char fouthByte = *(char*)(pData + iDataIndex);
    iDataIndex += 1;

    qint32 iValue = (firstByte << 24) + (secondByte << 16) + (thirdByte << 8) + fouthByte;

    return iValue;
}

//从缓冲区中解析连续4个字节的数据为float值(fValue=四字节+三字节+次字节+首字节,指针pData位置没有发送变化)
float CBufferUtil::GetFloat(char* pData)
{
    if (0 == pData) return 0.0f;

    char pTempData[4] = { 0 };

    pTempData[0] = *(char*)(pData + 3);
    pTempData[1] = *(char*)(pData + 2);
    pTempData[2] = *(char*)(pData + 1);
    pTempData[3] = *(char*)(pData);

    float fValue = *(float*)pTempData;

    return fValue;
}

float CBufferUtil::DecodeFloat(char* pData, int& iDataIndex)
{
    if (0 == pData) return 0.0f;

    char pTempData[4] = { 0 };

    pTempData[3] = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    pTempData[2] = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    pTempData[1] = *(char*)(pData + iDataIndex);
    iDataIndex += 1;
    pTempData[0] = *(char*)(pData + iDataIndex);
    iDataIndex += 1;

    float fValue = *(float*)pTempData;

    return fValue;
}

//将quint16值加入到缓冲区中(quint16值的高字节为首字节，quint16值的低字节为次字节)
void CBufferUtil::AddQuint16(char* pData, quint16 ui16ParamVal)
{
    //首字节
    *pData = (char)(ui16ParamVal >> 8);
    ++pData;

    //次字节
    *pData = (char)(ui16ParamVal);
}

void CBufferUtil::EncodeQuint16(char* pData, quint16 ui16ParamVal, int& iDataIndex)
{
    //首字节
    *(pData+iDataIndex) = (char)(ui16ParamVal >> 8);
    iDataIndex += 1;

    //次字节
    *(pData+iDataIndex) = (char)(ui16ParamVal);
    iDataIndex += 1;
}

//将qint16值加入到缓冲区中(qint16值的高字节为首字节，qint16值的低字节为次字节)
void CBufferUtil::AddQint16(char* pData, qint16 i16ParamVal)
{
    //首字节
    *pData = (char)(i16ParamVal >> 8);
    ++pData;

    //次字节
    *pData = (char)(i16ParamVal);
}

void CBufferUtil::EncodeQint16(char* pData,  qint16 i16ParamVal, int& iDataIndex)
{
    //首字节
    *(pData+iDataIndex) = (char)(i16ParamVal >> 8);
    iDataIndex += 1;

    //次字节
    *(pData+iDataIndex) = (char)(i16ParamVal);
    iDataIndex += 1;
}

//将quint32值加入到缓冲区中
void CBufferUtil::AddQuint32(char* pData, quint32 ui32ParamVal)
{
    //首字节
    *pData = (char)(ui32ParamVal >> 24);
    ++pData;

    //次字节
    *pData = (char)(ui32ParamVal >> 16);
    ++pData;

    //第三字节
    *pData = (char)(ui32ParamVal >> 8);
    ++pData;

    //第四字节
    *pData = (char)(ui32ParamVal);
}

//static
void CBufferUtil::EncodeQuint32(char* pData, quint32 ui32ParamVal, int& iDataIndex)
{
    //首字节
    *(pData+iDataIndex) = (char)(ui32ParamVal >> 24);
    iDataIndex += 1;

    //次字节
    *(pData+iDataIndex) = (char)(ui32ParamVal >> 16);
    iDataIndex += 1;

    //第三字节
    *(pData+iDataIndex) = (char)(ui32ParamVal >> 8);
    iDataIndex += 1;

    //第四字节
    *(pData+iDataIndex) = (char)(ui32ParamVal);
    iDataIndex += 1;
}

//将qint32值加入到缓冲区中
void CBufferUtil::AddQint32(char* pData, qint32 i32ParamVal)
{
    //首字节
    *pData = (char)(i32ParamVal >> 24);
    ++pData;

    //次字节
    *pData = (char)(i32ParamVal >> 16);
    ++pData;

    //第三字节
    *pData = (char)(i32ParamVal >> 8);
    ++pData;

    //第四字节
    *pData = (char)(i32ParamVal);
}

void CBufferUtil::EncodeQint32(char* pData, qint32 i32ParamVal, int& iDataIndex)
{
    //首字节
    *(pData+iDataIndex) = (char)(i32ParamVal >> 24);
    iDataIndex += 1;

    //次字节
    *(pData+iDataIndex) = (char)(i32ParamVal >> 16);
    iDataIndex += 1;

    //第三字节
    *(pData+iDataIndex) = (char)(i32ParamVal >> 8);
    iDataIndex += 1;

    //第四字节
    *(pData+iDataIndex) = (char)(i32ParamVal);
    iDataIndex += 1;
}

//将float值加入到缓冲区中
void CBufferUtil::AddFloat(char* pData, float fParamVal)
{
    char aTemp[4];
    memcpy(aTemp, &fParamVal, 4);

    //首字节
    *pData = aTemp[3];
    ++pData;

    //次字节
    *pData = aTemp[2];
    ++pData;

    //第三字节
    *pData = aTemp[1];
    ++pData;

    //第四字节
    *pData = aTemp[0];
}

void CBufferUtil::EncodeFloat(char* pData, float fParamVal, int& iDataIndex)
{
    char aTemp[4];
    memcpy(aTemp, &fParamVal, 4);

    //首字节
    *(pData+iDataIndex) = aTemp[3];
    iDataIndex += 1;

    //次字节
    *(pData+iDataIndex) = aTemp[2];
    iDataIndex += 1;

    //第三字节
    *(pData+iDataIndex) = aTemp[1];
    iDataIndex += 1;

    //第四字节
    *(pData+iDataIndex) = aTemp[0];
    iDataIndex += 1;
}

//获取低16bit位(2个字节)
int CBufferUtil::GetLow16(qint32 iValue)
{
    int ret = 0;

    ret = iValue & 0xFFFF;

    return ret;
}

//获取高16bit位(2个字节)
int CBufferUtil::GetHigh16(qint32 iValue)
{
    int ret = 0;

    ret = iValue >> 16;

    return ret;
}

//获取第一个高字节0XAA(0xAABBCCDD)
quint8 CBufferUtil::GetByte1(quint32 iValue)
{
    quint8 ret = 0;

    ret = (iValue>>24) & 0xFF;

    return ret;
}

//获取第二个高字节0XBB(0xAABBCCDD)
quint8 CBufferUtil::GetByte2(quint32 iValue)
{
    quint8 ret = 0;

    ret = (iValue>>16) & 0x00FF;

    return ret;
}

//获取第三个高字节0XCC(0xAABBCCDD)
quint8 CBufferUtil::GetByte3(quint32 iValue)
{
    quint8 ret = 0;

    ret = (iValue>>8) & 0x0000FF;

    return ret;
}

//获取第四个高字节0XDD(0xAABBCCDD)
quint8 CBufferUtil::GetByte4(quint32 iValue)
{
    quint8 ret = 0;

    ret = iValue & 0x000000FF;

    return ret;
}

//获取低8bit位(1个字节)
quint8 CBufferUtil::GetLow8(quint16 iValue)
{
    quint8 ret = 0;

    ret = iValue & 0xFF;

    return ret;
}

//获取高8bit位(1个字节)
quint8 CBufferUtil::GetHigh8(quint16 iValue)
{
    int ret = 0;

    ret = iValue >> 8;

    return ret;
}
